package com.vader.juc.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author kaivader
 */
public class deadLock {
    public static void main(String[] args) {
        Lock a = new ReentrantLock();
        Lock b = new ReentrantLock();

        new Thread(()->{
          synchronized (a){
              System.out.println("持有锁a，试图获取锁b");

              try {
                  TimeUnit.SECONDS.sleep(1);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              synchronized (b){
                  System.out.println("成功获取到锁b");
              }
          }
        },"A").start();

        new Thread(()->{
            synchronized (b){
                System.out.println("持有锁b，试图获取锁a");

                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (a){
                    System.out.println("成功获取到锁a");
                }
            }
        },"B").start();
    }
}
